package org.skywind.WindSearch.servlet;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.skywind.Robot.interfaces.SpiderInterface;
import org.skywind.WindSearch.OperationHost;
import org.skywind.WindSearch.SearchManager;
import org.skywind.WindSearch.interfaces.Operation;

public class snapshot extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public snapshot() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String host = (String)request.getParameter("host");
		int robot = Integer.parseInt((String)request.getParameter("robot"));
		int id = Integer.parseInt((String)request.getParameter("id"));
		SearchManager sm = (SearchManager)this.getServletContext().getAttribute("system");
		////////
		OperationHost oh = (OperationHost)sm.getOprationPool().get(host);
		Operation oi = ((Operation)oh.getObjectPool().get("Searcher"));
		SpiderInterface si = (SpiderInterface)oi.getRobots().get(robot);
		BufferedImage image = ImageIO.read(ImageIO.createImageInputStream(new ByteArrayInputStream(si.getBreviaryPicture(id))));
		////////
		response.setContentType("image/jpeg");
		response.setHeader("Pragma","No-cache");
		response.setHeader("Cache-Control","no-cache");
		response.setDateHeader("Expires", 0);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "JPEG", out);
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
